package com.blb.permission_ms.controller;

import com.blb.permission_ms.entity.Permission;
import com.blb.permission_ms.entity.ResponseResult;
import com.blb.permission_ms.service.PermissionService;
import com.blb.permission_ms.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @RequestMapping("/permission-tree")
    public ResponseResult<List<Permission>> getPermissionsTree(){
        List<Permission> permissionsTree = permissionService.getPermissionsTree();
        return ResponseResult.ok(permissionsTree);
    }

    @RequestMapping("/permission-ids/{roleId}")
    public ResponseResult<List<Long>> getPermissionIdsByRoldId(@PathVariable Long roleId){
        List<Long> ids = permissionService.getPermissionIdsByRoleId(roleId);
        return ResponseResult.ok(ids);
    }

    @PutMapping("/role-permissions")
    public ResponseResult<String> updateRolePermissions(Long roleId,String permissionIds){
        System.out.println(roleId);
        System.out.println(permissionIds);
        rolePermissionService.updateRolePermissions(roleId,permissionIds);
        return ResponseResult.ok("ok");
    }
}
